package org.osomit.sacct.builder;

import org.osomit.sacct.entity.ApplicationAccountRecord;
import org.osomit.sacct.entity.ApplicationAccount;

import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 * 
 * Date: Jul 1, 2009
 */
public class ApplicationAccountRecordBuilder extends BaseBuilder<ApplicationAccountRecord> {

    public ApplicationAccountRecord build(Object object) {
        
        if (object != null) {
            ApplicationAccountRecord record = new ApplicationAccountRecord();
            Map map = (Map) object;
            record.setClientId((String) map.get(ApplicationAccountRecord.CLIENT_ID));

            List list = (List)map.get(ApplicationAccountRecord.ACCOUNTS);
            if(list != null && list.size() > 0){
                List<ApplicationAccount> accounts = new ArrayList<ApplicationAccount>();

                ApplicationAccountBuilder builder = new ApplicationAccountBuilder();
                for(Object obj: list){
                    ApplicationAccount acct = builder.build(obj);
                    accounts.add(acct);
                }

                record.setAccounts(accounts);
            }

            return record;
        }

        return null;
    }
}
